/**
 * Created by KhoaNam on 7/9/2014.
 */

var MessageBox = cc.Class.extend( {
    ctor: function(){
        //this._super();
        this.startLogoSprite = null;
        this.bkgSprite = null;
        this.parent = null;
        this.textSprite = null;
        this.isShow = false;
        this.button = null;
        this.typeButton = INFO.MESSAGEBOX.BUTTON_READY;

        this.buttonOk = null;
        this.buttonReplay = null;
        this.buttonJoinReplay = null;



    },
    init: function(){

    },

    getStartLogoSprite: function(){
        if(this.startLogoSprite == null){
            this.startLogoSprite = cc.Sprite.create(MESSAGEBOX.START_LOGO);
        }
        return this.startLogoSprite;
    },

    getBackgroundSprite: function(){
        if(this.bkgSprite == null){
            this.bkgSprite = cc.Sprite.create(MESSAGEBOX.BACKGROUND);
        }
        return this.bkgSprite;
    },

    getTextSprite: function(){

        if(this.textSprite == null){
            this.textSprite = cc.LabelTTF.create("",
                MESSAGEBOX.FONT, MESSAGEBOX.FONT_SIZE, cc.size(this.getBackgroundSprite().getContentSize().width , this.getBackgroundSprite().getContentSize().height), cc.TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_CENTER);
        }
        return this.textSprite;
    },

    setText : function(text){
        this.getTextSprite().setString(text);
    },

    _getStartLogoPosition: function(){
        var position = this.getBackgroundSprite().getPosition();
        position.y += this.getBackgroundSprite().getContentSize().height/2;
        return position;
    },

    getButtonSprite: function(){
        if(this.button == null){
            var data = INFO.BUTTON.READY;
            switch (this.typeButton){
                case INFO.MESSAGEBOX.BUTTON_READY:
                    data = INFO.BUTTON.READY;
                    break;
                case INFO.MESSAGEBOX.BUTTON_START:
                    data = INFO.BUTTON.START;
                    break;
                case INFO.MESSAGEBOX.BUTTON_QUIT:

                    break;
            }

            this.button = new MyButton(data);
        }
        return this.button;
    },

    addToLayer: function(layer){
        this.parent = layer;
    },

    showHOA: function( posX, posY,text){
        if(this.parent != null){
            console.log("show hoa");
            this.parent.addChild(this.getBackgroundSprite(), INFO.TILE.ORDER_MESSAGE_DIALOG);
            this.parent.addChild(this.getStartLogoSprite(), INFO.TILE.ORDER_MESSAGE_DIALOG);
            this.parent.addChild(this.getTextSprite(), INFO.TILE.ORDER_MESSAGE_DIALOG);
            this.getTextSprite().setPosition(posX, posY);
            this.getBackgroundSprite().setPosition(posX, posY);
            var pos = this._getStartLogoPosition();
            this.getStartLogoSprite().setPosition(pos.x, pos.y);

            this.getTextSprite().setString(text);
        }
    },

    showResult: function(players, posX, posY){
        if(this.parent != null) {
            var x = posX - this.getBackgroundSprite().getBoundingBox().getWidth() / 2;
            var y = posY + this.getBackgroundSprite().getBoundingBox().getHeight() / 2;
            var iconWidth = 48;
            var nameWidth = (this.getBackgroundSprite().getContentSize().width - 20 - 20 - iconWidth) / 3  + 20;
            var pointWidth = (this.getBackgroundSprite().getContentSize().width - 20 - 20 - iconWidth) / 3;
            var moneyWidth = (this.getBackgroundSprite().getContentSize().width - 20 - 20 - iconWidth) / 3;

            var heightLabel = 30;

            x += 10;
            y -= 90;

            for (var i = 0; i < players.length; i++) {
                var nameLabel = cc.LabelTTF.create(players[i].name, INFO.MESSAGEBOX.FONT, 14, cc.size(nameWidth, heightLabel), cc.TEXT_ALIGNMENT_LEFT, cc.VERTICAL_TEXT_ALIGNMENT_CENTER);
                var pointLabel = cc.LabelTTF.create(players[i].point, INFO.MESSAGEBOX.FONT, 14, cc.size(pointWidth, heightLabel), cc.TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_CENTER);
                var moneyLabel;
                if(players[i].money < 0){
                    moneyLabel = cc.LabelTTF.create("-$" + (players[i].money*-1), INFO.MESSAGEBOX.FONT, 14, cc.size(moneyWidth, heightLabel), cc.TEXT_ALIGNMENT_RIGHT, cc.VERTICAL_TEXT_ALIGNMENT_CENTER);
                }
                else{
                    moneyLabel = cc.LabelTTF.create("$" + players[i].money, INFO.MESSAGEBOX.FONT, 14, cc.size(moneyWidth, heightLabel), cc.TEXT_ALIGNMENT_RIGHT, cc.VERTICAL_TEXT_ALIGNMENT_CENTER);
                }

                var icon;
                if(players[i].isWinner){
                    icon = cc.Sprite.create(INFO.ICONS.WIN);
                }
                else{
                    icon = cc.Sprite.create(INFO.ICONS.LOSE);
                }

                nameLabel.setPosition(x + iconWidth + nameWidth / 2, y - (i * heightLabel) - heightLabel / 2);
                pointLabel.setPosition(x + iconWidth + nameWidth + pointWidth / 2, y - (i * heightLabel) - heightLabel / 2);
                moneyLabel.setPosition(x + iconWidth + nameWidth + pointWidth + moneyWidth / 2, y - (i * heightLabel) - heightLabel / 2);
                icon.setPosition(x  + iconWidth/2,  y - (i * heightLabel) - heightLabel / 2);

                this.parent.addChild(icon, INFO.TILE.ORDER_MESSAGE_DIALOG + 11);
                this.parent.addChild(nameLabel, INFO.TILE.ORDER_MESSAGE_DIALOG + 10);
                this.parent.addChild(pointLabel, INFO.TILE.ORDER_MESSAGE_DIALOG + 10);
                this.parent.addChild(moneyLabel, INFO.TILE.ORDER_MESSAGE_DIALOG + 10);

                this.getBackgroundSprite().setPosition(posX, posY);
                var pos = this._getStartLogoPosition();
                this.getStartLogoSprite().setPosition(pos.x, pos.y);

                this.parent.addChild(this.getBackgroundSprite(), INFO.TILE.ORDER_MESSAGE_DIALOG);
                this.parent.addChild(this.getStartLogoSprite(), INFO.TILE.ORDER_MESSAGE_DIALOG);
            }
        }
    },

    show: function (x, y) {
        if(this.parent != null) {
            this.isShow = true;
            this.parent.addChild(this.getBackgroundSprite(), INFO.TILE.ORDER_MESSAGE_DIALOG);
            this.parent.addChild(this.getStartLogoSprite(), INFO.TILE.ORDER_MESSAGE_DIALOG);
            this.parent.addChild(this.getTextSprite(), INFO.TILE.ORDER_MESSAGE_DIALOG);
            this.getTextSprite().setPosition(x, y);
            this.getBackgroundSprite().setPosition(x, y);
            var pos = this._getStartLogoPosition();
            this.getStartLogoSprite().setPosition(pos.x, pos.y);
        }
    },

    hide: function() {
        if (this.parent != null && this.isShow) {
            this.isShow = false;
            this.parent.removeChild(this.getBackgroundSprite());
            this.parent.removeChild(this.getStartLogoSprite());
            this.parent.removeChild(this.getTextSprite());

        }
    },

    getButtonOk : function(){
        if(this.buttonOk == null){
            this.buttonOk = new MyButton(INFO.BUTTON.OK);
        }
        return this.buttonOk;
    },

    showButtonOk: function(){
        if(this.parent != null){
            var pos = this.getBackgroundSprite().getPosition();
            pos.y = pos.y - this.getBackgroundSprite().getContentSize().height/2 + INFO.MESSAGEBOX.PADDING +  this.getButtonSprite().getContentSize().height/2;
            pos.x = pos.x - 100;
            this.getButtonOk().setPosition(cc.p(pos.x, pos.y));
            this.parent.addChildHasEvent(this.getButtonOk(), INFO.TILE.ORDER_MESSAGE_DIALOG + 5);
        }
    },

    hideButtonOk: function(){
        if(this.parent != null){
            if(this.buttonOk != null){
                this.parent.removeChildHasEvent(this.buttonOk);
            }
        }
    },

    getButtonReplay: function(){
        if(this.buttonReplay == null){
            this.buttonReplay = new MyButton(INFO.BUTTON.REPLAY);
        }
        return this.buttonReplay;
    },

    showButtonReplay: function(){
        if(this.parent != null){
            var pos = this.getBackgroundSprite().getPosition();
            pos.y = pos.y - this.getBackgroundSprite().getContentSize().height/2 + INFO.MESSAGEBOX.PADDING +  this.getButtonSprite().getContentSize().height/2;
            pos.x = pos.x + 100;
            this.getButtonReplay().setPosition(cc.p(pos.x, pos.y));
            this.parent.addChildHasEvent(this.getButtonReplay(), INFO.TILE.ORDER_MESSAGE_DIALOG + 5);
        }
    },

    hideButtonReplay: function(){
        if(this.parent != null){
            if(this.buttonReplay != null){
                this.parent.removeChildHasEvent(this.buttonReplay);
            }
        }
    },

    getButtonJoinRoomReplay: function (){
        if(this.buttonJoinReplay == null){
            this.buttonJoinReplay = new MyButton(INFO.BUTTON.JOIN_ROOM_REPLAY);
        }
        return this.buttonJoinReplay;
    },

    showButtonJoinReplay: function(){
        if(this.parent != null){
            var pos = this.getBackgroundSprite().getPosition();
            pos.y = pos.y - this.getBackgroundSprite().getContentSize().height/2 + INFO.MESSAGEBOX.PADDING +  this.getButtonSprite().getContentSize().height/2;
            pos.x = pos.x + 100;
            this.getButtonJoinRoomReplay().setPosition(cc.p(pos.x, pos.y));
            this.parent.addChildHasEvent(this.getButtonJoinRoomReplay(), INFO.TILE.ORDER_MESSAGE_DIALOG + 5);
        }
    },

    hideButtonReplay: function(){
        if(this.parent != null){
            if(this.buttonJoinReplay != null){
                this.parent.removeChildHasEvent(this.buttonJoinReplay);
            }
        }
    },

    showButton: function(typeButton){
        if(typeButton != undefined) {
            this.typeButton = typeButton;
        }

        if(this.parent != null){
            var pos = this.getBackgroundSprite().getPosition();
            pos.y = pos.y - this.getBackgroundSprite().getContentSize().height/2 + INFO.MESSAGEBOX.PADDING +  this.getButtonSprite().getContentSize().height/2;
            this.getButtonSprite().setPosition(pos.x, pos.y);
            this.parent.addChildHasEvent(this.getButtonSprite(), INFO.TILE.ORDER_MESSAGE_DIALOG + 5);
        }
    },

    hideButton: function(){
        if(this.parent != null){
            if(this.button != null){
                this.parent.removeChildHasEvent(this.getButtonSprite());
                delete this.button;
                this.button = null;
            }
        }
    }
});

